<!--  -->
<template>
    <div>
<!--内容-->
<br>
<br>
<div class="m-container">
    <div class="ui container m-pad-tb-big m-opacity ">
        <div class="ui segment">
            <div class="ui form" >
                <div class="required field m-pad-tb-massive" style="max-width: 45em !important; margin: auto !important;">
                    <div class="ui left labeled input required ">
                        <label class="ui teal basic label">名称</label>
                        <input type="text" placeholder="请输入分类名称" v-model="type.name">
                    </div>
                </div>
                <div class="ui negative message" v-show="error">
                    <i class="close icon" @click="closeMsg"></i>
                    <div class="header">提示:</div>
                    <p>{{message}}</p>
                </div>
                <div class="ui center aligned container m-pad-bottom-max">
                    <button type="button" class="ui button" onclick="window.history.go(-1)">返回</button>
                    <button class="ui teal submit button" @click="submitType" v-if="type.id==0">提交</button>
                    <button class="ui teal submit button" @click="submitType" v-if="type.id!=0">修改</button>
                </div>
            </div>
        </div>
    </div>
</div>
<br>
<br>
<br>
<br>
<br>
<br>
    </div>
</template>

<script>
export default {
    data () {
        return {
            type:"",
            error:false,
            message:""
        };
    },
    methods:{
        submitType(){
            var url
            if(this.type.id == 0)
                url = "/admin/saveType"
            else
                url = "/admin/updateType"
            var _this = this
            //非空验证
            if(_this.type.name.length==0){
                _this.message="请正确输入信息"
                _this.error=true
                return
            }
            _this.axios.post(url,{
                id:_this.type.id,
                name:_this.type.name
            }).then(res =>{
                if(res.data.status === 200){
                    alert(res.data.msg)
                    _this.$router.push({path:"/admin/types"})
                }else{
                    _this.message=res.data.msg
                    _this.error=true
                }
            }).catch(e =>{
                console.log(e)
            })
        },
        closeMsg(){
            this.error = false
        }
    },
    created(){
        this.type = this.$store.state.editType
    }
}

</script>
<style lang='scss' scoped>
</style>